
<jsp:useBean id="user" class="bean.PollUser" scope="request" />
<jsp:setProperty name="user" property="*"/>
<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="GBK" import="bean.PollUser, model.PollUserImpl" %>

<%
	// Get user bean all setup...
	//session.setAttribute("user",user);
	
	// Redirect to login page if user not logged in
	if (user.getUsername() == null) {
		response.sendRedirect("login.jsp");
	}
	if(session.getAttribute("Accessibility") != null && session.getAttribute("Accessibility").equals("1")){
		response.sendRedirect("success.jsp");
	}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"> 
<link rel="stylesheet" href="style/stylesheet.css" type="text/css" >
<title>dbPoll/User Administration: Create User</title>
<script type="text/javascript">
	var emailList = "";

    function check(form){   
        if(document.forms.form1.username.value==""){
            document.getElementById("username_msg").innerHTML = "<div class=\"error\" style=\"display: block;\" >* Please insert username. </div>";
            document.forms.form1.username.focus();   
            return false;   
        } else {
        	document.getElementById("username_msg").innerHTML = "";
        }
               
        if(document.forms.form1.email.value=="" && document.getElementById("email_list").innerHTML == "" ) {   
            document.getElementById("email_msg").innerHTML = "<div class=\"error\" style=\"display: block;\" >* Please insert email. </div>";
            document.forms.form1.email.focus();   
            return false;   
        } else {
        	document.getElementById("email_msg").innerHTML = "";
        }
        return true;
    }

    function user_type() {
        if (document.forms.form1.accessibility.value == 1) {
            document.getElementById("single_generic_radio_div").innerHTML = "<td></td><td><input type=\"radio\" name=\"single_generic\" id=\"single_generic\" value=\"s\" checked=\"yes\" onclick=\"generic_selected();\">Single</input><input type=\"radio\" name=\"single_generic\" id=\"single_generic\" value=\"g\" onclick=\"generic_selected();\">Generic</input></td>";
        } else {
        	document.getElementById("single_generic_radio_div").innerHTML = "";
			document.getElementById("email_list").innerHTML = "";
			document.getElementById("add_email_to_list_button").style.visibility = "hidden";
			//document.getElementById("add_email_to_list_button").style.visibility = "hidden";
        }
    }

    function generic_selected() {
		//alert ("generic_selected() function run");
		
		//Check if selected is generic
		if (document.forms.form1.single_generic[1].checked) {
			document.getElementById("email_list").innerHTML = "<td>Email List</td><td id=\"email_email\" ></td>";
			document.getElementById("add_email_to_list_button").style.visibility = "visible";
			document.getElementById("add_email_to_list_button").style.visibility = "visible";
		} else {
			document.getElementById("email_list").innerHTML = "";
			document.getElementById("add_email_to_list_button").style.visibility = "hidden";
			//document.getElementById("add_email_to_list_button").style.visibility = "hidden";
		}
    }

    function add_email_to_list() {

        // Check if not empty
		if (document.forms.form1.email.value != "") {
			// Add to list
			if (emailList == "" ) {
				emailList += document.forms.form1.email.value; 
			} else {
				emailList += "," + document.forms.form1.email.value;
			}
			document.getElementById("email_email").innerHTML = emailList;
			document.forms.form1.hidden_email_list.value = emailList;
			document.getElementById("email_input_field").value = "";
		}
    }
   

</script>
</head>
<body onload="user_type();">
<div class="bg">
<div class='content' >
<jsp:include page="nav.jsp" />
	<div id = "title" class= "pagetitle">User Administration<input type="button" style="font-size:8pt;color:white;background-color:lightblue;border:none;" value="Online Help"></div>
	<div class="entrytitle">
	Create User
	<div class ="entry">
	
	<form action="CreateUserServlet" method="post" name="form1">  
		<table>
			<tr><td width="100">User Type</td><td><select name="accessibility" onchange="user_type();">
			<%	
				String attribute = (session.getAttribute("accessibility").toString());
			 	switch (Integer.parseInt(attribute)) {
			 		case 5:
			 			out.println("<option value=\"5\">System Administrator</option>");
			 			out.println("<option value=\"4\">Poll Adminstrator</option>");
			 		case 4:
			 			out.println("<option value=\"3\">Poll Creator</option>");
			 		case 3:
			 			out.println("<option value=\"2\">Poll Master</option>");
			 		case 2:
			 			out.println("<option value=\"1\">Poll User</option>");
			 		default:
			 			;
			 	}
			 	
			 %>
			</select></td></tr>
			
			<tr id="single_generic_radio_div"></tr>
			
			<tr><td>Username</td><td><input type="text" size="50" name="username" id="username"/></td><td><div id="username_msg"></div>
			<%
				// Check for taken username
				if (session.getAttribute("CreateUserUsernameTaken") != null && session.getAttribute("CreateUserUsernameTaken").equals("t")) {
					out.println ("<div class=\"error\" style=\"display: block;\" >* The username submitted is taken.</div>");
				}
			 %></td></tr>
			 	 
			<tr><td>Email</td><td><input type="text" size="50" id="email_input_field"name="email"/></td><td><div id="email_msg"></div>
			<%
				// Check for incorrect username submitted
				
			 %></td>
			 
			<td></td><td><input type="button" id="add_email_to_list_button" class="button" value="Add email to list" style="visibility: hidden" onclick="add_email_to_list();" /></td></tr>
			 
			<tr id="email_list"></tr>
			
			<tr><td></td><td><input type="text" name="hidden_email_list" style="visibility: hidden" /></td></tr>
			 
			<tr> <td></td><td><input type="submit" name="submit" class="button" value="Create user" onclick="return check(this);" /></td> </tr>
			
			</table>
			
			</form>  
			<%	
				// Check for success/failure message
				if (request.getParameter("UserCreatedSuccess") != null && request.getParameter("UserCreatedSuccess").equals("t")) {
					out.println("<div class=\"note\" style=\"display: block;\" >* User successfully created!</div>");
				}
				if (request.getParameter("UserCreatedSuccess") != null && request.getParameter("UserCreatedSuccess").equals("f")) {
					out.println("<div class=\"error\" style=\"display: block;\" >* Failed to create user!</div>");
				}
			%>	

</div>
</div>
</div>
</div>
<style>
#onlinehelp{position:absolute;display:none;z-index:2;border:5px solid lightblue;background-color:#33aecc;color:white}
</style>
<div id="onlinehelp"></div>
<script>
var demo = document.getElementById("title");
var gg = demo.getElementsByTagName("input");
var ei = document.getElementById("onlinehelp");
for(i=0; i<gg.length; i++){
 var ts = gg[i];
 ts.onmousemove = function(event){
  event = event || window.event;
  ei.style.display = "block";
  ei.innerHTML = '1. Select User Type form dropdown list<br>2. Input the username and email,the system will generate a random password and send email<br> to the user with account information <br> * For more help please click the button';
  ei.style.top  = document.body.scrollTop + event.clientY  -70 + "px";
  ei.style.left = document.body.scrollLeft + event.clientX  +30 + "px";
 }
 ts.onmouseout = function(){
  ei.innerHTML = "";
  ei.style.display = "none";
 }
 ts.onclick = function(){
  window.open( this.src );
 }
}
</script>
</body>
</html>